package com.monitor.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.monitor.commons.HttpTookit;
import com.monitor.commons.YXDate;
import com.monitor.config.yunke.YunKeConfig;
import com.monitor.config.yunke.YunKeUtil;
import com.monitor.dao.ICallRecordsDao;
import com.monitor.modelExt.CallRecordsExt;
import com.monitor.pubInter.AbstractBaseService;
import com.monitor.pubInter.IBaseDao;
import com.monitor.pubInter.exception.SkException;
import com.monitor.searchExt.CallRecordsSearchExt;
import com.monitor.service.ICallRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * @author afeng
 * @Date 2025-03-17 10:50:14
 * @description CallRecordsService      通话记录
 */
@Service(value = "callRecordsService")
@Slf4j
public class CallRecordsServiceImpl extends AbstractBaseService<CallRecordsExt, CallRecordsSearchExt, String> implements ICallRecordsService {

    @Resource(name = "callRecordsDao")
    private ICallRecordsDao callRecordsDao;

    @Autowired
    private YunKeUtil yunKeUtil;
    @Autowired
    private YunKeConfig yunKeConfig;

    @Override
    public IBaseDao<CallRecordsExt, CallRecordsSearchExt, String> getDao() {
        return callRecordsDao;
    }

    /**
     * 同步通话记录数据
     *
     * @param syncDay 同步日期
     * @throws SkException
     */
    @Override
    public void syncCallRecordData(Date syncDay) throws SkException {
        String startTime = YXDate.getFormatDate2String(YXDate.getTimeDayFirstSecond(syncDay));
        String endTime = YXDate.getFormatDate2String(YXDate.getTimeDayLastSecond(syncDay));
        callRecordData(1, startTime, endTime);
    }


    private void callRecordData(int page, String startTime, String endTime) {
        try {
            //定义请求路径
            String url = yunKeConfig.getUrl() + "open/call/records";
            //组装请求头
            long timestamp = new Date().getTime();
            Map<String, String> headsMap = yunKeUtil.getHeadMap(timestamp);
            //组装请求参数
            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("page", page);
            paramsMap.put("size", 100);
            paramsMap.put("beginTime", startTime);
            paramsMap.put("endTime", endTime);
            String res = HttpTookit.doPostHeader(url, paramsMap, headsMap);
            log.info("通话记录数据:" + res);
            JSONObject resObject = JSONObject.parseObject(res);
            if (resObject.getBoolean("success")) {

                JSONObject dataObj = resObject.getJSONObject("data");
                int currentPage = dataObj.getInteger("pageIndex");
                int totalPage = dataObj.getInteger("pageCount");
                JSONArray dataArr = dataObj.getJSONArray("page");
                List<CallRecordsExt> callRecordsExtList = new ArrayList<>();
                dataArr.forEach((data) -> {
                    JSONObject item = JSONObject.from(data);
                    CallRecordsExt callRecord = JSONObject.parseObject(JSONObject.toJSONString(data), CallRecordsExt.class);
                    callRecord.setId(null);
                    callRecord.setUserId(item.getString("userId"));
                    callRecord.setYkId(item.getString("id"));
                    callRecord.setYkUserId(item.getString("yunkeUserId"));
                    callRecordsExtList.add(callRecord);
                });
                callRecordsDao.batchInsert(callRecordsExtList);

                log.info("第" + page + "页的数据:" + dataArr.size() + "条");
                if (totalPage > currentPage) {
                    currentPage++;
                    callRecordData(currentPage, startTime, endTime);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}